<template>
  <el-dialog :title="title" v-model="isShow" width="45%" @close="closeHandler" draggable="true">
    <el-form :model="formData" :rules="formRules" ref="formRef" label-width="120px" :inline="false">
      <el-form-item label="用户名" prop="nickname">
        <el-input v-model="formData.nickname"></el-input>
      </el-form-item>
      <el-form-item label="昵称" prop="username">
        <el-input v-model="formData.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="formData.password"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="formData.email"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="formData.phone"></el-input>
      </el-form-item>
      <el-form-item label="角色" prop="roles">
        <el-select v-model="formData.roles" multiple collapse-tags collapse-tags-tooltip :max-collapse-tags="3"
          placeholder="Select" style="width: 240px">
          <el-option v-for="item in roleList" :key="item.key" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="启用状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio :label="0">禁用</el-radio>
          <el-radio :label="1">启用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="头像" prop="avatar">
        <Upload2 v-model="formData.avatar" :type="'user'" />
      </el-form-item>
    </el-form>
    <el-button type="primary" @click="onSubmit">确定</el-button>
    <el-button type="primary" plain>取消</el-button>
  </el-dialog>
</template>
<script setup>
import { ref, computed, toRaw, onMounted } from 'vue'
import { ElMessage } from "element-plus";
import Upload2 from '../../../components/Xf/Upload2.vue'
import { addUserApi, getUserInfoApi, editUserApi } from '../../../api/user'
const isShow = ref(false)
const formRef = ref()
const defaultState = {
  nickname: '',
  username: '',
  password: '',
  email: '',
  phone: '',
  status: 1,
  roles: '',
  id: null,
  avatar: ''
}
const emit = defineEmits(["updateList"]);
defineProps({
  roleList: {
    type: Array,
    default: () => []
  }
})
const formData = reactive({ ...defaultState });
const onSubmit = async () => {
  const data = toRaw(formData);
  // await addUserApi(data)
  await formRef.value.validate();
  formData.id ? await editUserApi(data) : await addUserApi(data)
  ElMessage.success("操作成功");
  emit("updateList");
  isShow.value = false;
}
const formRules = {
  nickname: [{ required: true, message: "请选择", trigger: "blur" }],
  name: [{ required: true, message: "请选择", trigger: "blur" }],
  email: [{ required: true, message: "请选择", trigger: "blur" }],
  phone: [{ required: true, message: "请选择", trigger: "blur" }],
  password: [{ required: true, message: "请选择", trigger: "blur" }],
  status: [{ required: true, message: "请选择", trigger: "blur" }],
  roles: [{ required: true, message: "请选择", trigger: "blur" }],
  avatar: [{ required: true, message: "请选择", trigger: "change" }],
};
const replyData = async (id) => {
  let res = await getUserInfoApi(id)
  const str = res.data.roles
  const arr = str.split(',')
  const roles = arr.map(item => item * 1)
  isShow.value = true
  Object.assign(formData, {
    nickname: res.data.nickname,
    username: res.data.username,
    password: res.data.password,
    email: res.data.email,
    phone: res.data.phone,
    status: res.data.status,
    roles: roles,
    id: res.data.id,
    avatar: res.data.avatar
  })
  console.log(formData);
}
const closeHandler = () => {
  // 移除校验
  // 重置表单数据
  formRef.value.resetFields()
  Object.assign(formData, { ...defaultState })
};
const title = computed(() => {
  return formData.id ? '修改' : '新增'
})
defineExpose({
  isShow,
  replyData
})
</script>